<template>
  <div style="margin-top: 50px">
    <!-- 上传楼盘图片 -->
    <p>为便于用户了解楼盘精确下单，需要展示楼盘图片供参考</p>
    <el-form :model="value" ref="productSaleForm" label-width="120px" class="form-inner-container" size="small" >
      <el-form-item label="楼盘图片:" prop="url">
        <ImageUpload v-model="value.url" @change="getAvatarUrl"  :isbig="true"/>
      </el-form-item>
      <el-form-item style="text-align: center">
        <el-button size="medium" @click="handlePrev">上一步，填写楼盘信息</el-button>
        <el-button type="primary" size="medium" @click="handleNext">下一步，添加电梯信息</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {fetchList as fetchMemberLevelList} from '@/api/memberLevel'
  import ImageUpload from "@/components/image-upload";
import {getUpToken,uploadQNImage} from "@/api/auser"
import qiniu from "@/utils/qiniu";

  export default {
    name: "ProductSaleDetail",
    components: {
    ImageUpload
  },
  
    props: {
      value: Object,
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        //日期选择器配置
        pickerOptions1: {
          disabledDate(time) {
            return time.getTime() < Date.now();
          }
        }
      }
    },
    created() {
      // if (this.isEdit) {
      //   // this.handleEditCreated();
      // } else {
      //   fetchMemberLevelList({defaultStatus: 0}).then(response => {
      //     let memberPriceList = [];
      //     for (let i = 0; i < response.data.length; i++) {
      //       let item = response.data[i];
      //       memberPriceList.push({memberLevelId: item.id, memberLevelName: item.name})
      //     }
      //     this.value.memberPriceList = memberPriceList;
      //   });
      // }
    },
    computed: {
      //选中的服务保证
      selectServiceList: {
        get() {
          let list = [];
          if (this.value.serviceIds === undefined || this.value.serviceIds == null || this.value.serviceIds === '') return list;
          let ids = this.value.serviceIds.split(',');
          for (let i = 0; i < ids.length; i++) {
            list.push(Number(ids[i]));
          }
          return list;
        },
        set(newValue) {
          let serviceIds = '';
          if (newValue != null && newValue.length > 0) {
            for (let i = 0; i < newValue.length; i++) {
              serviceIds += newValue[i] + ',';
            }
            if (serviceIds.endsWith(',')) {
              serviceIds = serviceIds.substr(0, serviceIds.length - 1)
            }
            this.value.serviceIds = serviceIds;
          } else {
            this.value.serviceIds = null;
          }
        }
      }
    },
    methods: {

      getAvatarUrl(file) {
      console.log("图片上传", file);

      if (file) {
        getUpToken()
          .then(res => {
            return uploadQNImage({
              token: res.uptoken,
              file: file.raw
            });
          })
          .then(res => {
            if (res.key) {
              this.$set(
                this.value,
                "url",
                `${qiniu.domain}${res.key}`
              );
            }
            console.log('qiniu',`${qiniu.domain}${res.key}`);
          });
      }
    },
      handleEditCreated() {
        let ids = this.value.serviceIds.split(',');
        console.log('handleEditCreated', ids);
        for (let i = 0; i < ids.length; i++) {
          this.selectServiceList.push(Number(ids[i]));
        }
      },
      handleRemoveProductLadder(index, row) {
        let productLadderList = this.value.productLadderList;
        if (productLadderList.length === 1) {
          productLadderList.pop();
          productLadderList.push({
            count: 0,
            discount: 0,
            price: 0
          })
        } else {
          productLadderList.splice(index, 1);
        }
      },
      handleAddProductLadder(index, row) {
        let productLadderList = this.value.productLadderList;
        if (productLadderList.length < 3) {
          productLadderList.push({
            count: 0,
            discount: 0,
            price: 0
          })
        } else {
          this.$message({
            message: '最多只能添加三条',
            type: 'warning'
          });
        }
      },
      handleRemoveFullReduction(index, row) {
        let fullReductionList = this.value.productFullReductionList;
        if (fullReductionList.length === 1) {
          fullReductionList.pop();
          fullReductionList.push({
            fullPrice: 0,
            reducePrice: 0
          });
        } else {
          fullReductionList.splice(index, 1);
        }
      },
      handleAddFullReduction(index, row) {
        let fullReductionList = this.value.productFullReductionList;
        if (fullReductionList.length < 3) {
          fullReductionList.push({
            fullPrice: 0,
            reducePrice: 0
          });
        } else {
          this.$message({
            message: '最多只能添加三条',
            type: 'warning'
          });
        }
      },
      handlePrev() {
        this.$emit('prevStep')
       

    },
      handleNext() {
        this.$emit('nextStep')
         // 对应展示电梯置空元素
         console.log(this.value.lift);
         if(Number(this.value.lift)>this.value.liftArr.length){
          // 编辑时修改了电梯数量
          const add=Number(this.value.lift)-this.value.liftArr.length
          for(let i=0;i<add;i++){
            this.value.liftArr.push({name:'',url:'',check:'false'})
          }
         }else if(Number(this.value.lift)<this.value.liftArr.length){
          const del=this.value.liftArr.length-Number(this.value.lift)
          for(let i=0;i<del;i++){
            this.value.liftArr.pop()
          }
         }
         if(!this.isEdit)
        {
          // 添加楼盘时候
           this.value.liftArr=[]
          for(let i=0;i<this.value.lift;i++){
          this.value.liftArr.push({name:'',url:'',check:'false'})
      }
        }
       
      console.log('电梯数组', this.value.liftArr);
      }
    }
  }
</script>

<style scoped>
  .littleMargin {
    margin-top: 10px;
  }
</style>
